package net.starelement.digwartnt;

import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.block.Block;
import cn.nukkit.event.EventHandler;
import cn.nukkit.event.entity.*;
import cn.nukkit.event.player.PlayerChatEvent;
import cn.nukkit.event.player.PlayerMoveEvent;
import cn.nukkit.item.Item;
import cn.nukkit.level.Explosion;
import cn.nukkit.level.Position;
import net.starelement.digwartnt.game.Game;
import net.starelement.digwartnt.game.GameReady;
import net.starelement.digwartnt.game.Ground;
import net.starelement.gamelib.player.PlayerList;

import java.util.List;

/**  StarElement DigwarTNT 事件监听器类
 *
 * @author Ruok233
 * @since NukkitX API 1.0.9 | StarElement Project v1-alpaca
 *
 * create: 2019/12/05
 * update: 2019/12/08
 */

public class Listener implements cn.nukkit.event.Listener {

    //当箭射中方块时发生爆炸 威力 0.5D
    @EventHandler
    public void hitBlock(ProjectileHitEvent event) {
        Position position = event.getEntity().getPosition();
        event.getEntity().kill();
        try {
            Explosion explosion = new Explosion(position, 0.5D, event.getEntity());
            explosion.explodeA();
            explosion.explodeB();
        } catch (Exception e) {}
    }


    //当箭命中实体时爆炸  威力 1D
    //爆炸者为箭，并非被射中的实体，且射中创造玩家无效
    @EventHandler
    public void hiEntity(EntityDamageByChildEntityEvent event) {
        event.setCancelled(true);
        Position position = event.getEntity().getPosition();
        Explosion explosion = new Explosion(position, 1D, event.getChild());
        explosion.explodeA();
        explosion.explodeB();
    }

    //确保玩家不会被炸死
    @EventHandler
    public void explodeTNT(EntityDamageByEntityEvent event) {
        event.setCancelled();
    }


    //阻止除了箭以外的实体发生爆炸
    //阻止除了TNT以外的方块被炸掉 注意，这个实现有BUG，还会存在有地图被炸的情况
    @EventHandler
    public void dontExplode(EntityExplodeEvent event) {
        if (!event.getEntity().getName().equals("Arrow")) {
            event.setCancelled(true);
        }

        List<Block> blockList = event.getBlockList();               //获取会被炸掉的方块列表
        if (event.getEntity().getName().equals("Arrow")) {
            try {
                if (!blockList.get(0).getName().equals("TNT")) {    //如果不会炸到TNT就阻止此次爆炸
                    event.setCancelled();
                }
            } catch (Exception e) { event.setCancelled(); }         //如果什么也没炸到就阻止此次爆炸
        }
    }


    //自动填充箭
    @EventHandler
    public void shootBow(EntityShootBowEvent event) {
        String name = event.getEntity().getName();

        //冒险模式射箭不扣除箭?!
        //只有在生存模式才自动填充箭；不过好像不允许切换到生存，这个用处不大
        Player player = Server.getInstance().getPlayer(name);
        if (player.gamemode == 0) {
            player.getInventory().addItem(Item.get(262, 0, 1));
        }
    }

    //检测玩家是否掉下去
    @EventHandler
    public void playerMove(PlayerMoveEvent event) {
        double y = event.getTo().getY();
        Player player = event.getPlayer();
        if (y < 2D) {
//            player.teleport(event.getPlayer().getLevel().getSafeSpawn());
            player.sendTitle("§b§c你失败了！");
            player.getServer().broadcastMessage("§e" + player.getName() + "§f失败了！");
            player.getInventory().clearAll();

            //临时代码
            PlayerList.getInstance().teleport(event.getPlayer().getLevel().getSafeSpawn());
            Ground.getInstance().clear();
            GameReady.list.clear();
        }
    }

    //临时启动入口
    @EventHandler
    public void playerChat(PlayerChatEvent event) {
        if (event.getMessage().equals("开始")) {
            Server.getInstance().getLogger().info("开始");
            new Game().start();
        }
    }


}
